<?php
class Model_telequestion extends Model {
    function Model_telequestion() {
        parent::Model();
    }

    /**
     * Functie care aduce datele in grid
     */
    function getData($aPost) {

        $this->db->start_cache();
        $iStart = (isset($aPost["start"]) && ($aPost["start"] != "")) ? $aPost["start"] : 0;
        $iLimit = (isset($aPost["limit"]) && ($aPost["limit"] != "")) ? $aPost["limit"] : 100;
        $sSort = (isset($aPost["sort"]) && ($aPost["sort"] != "")) ? $aPost["sort"] : "tq_name";
        $sDir = (isset($aPost["dir"]) && ($aPost["dir"] != "")) ? $aPost["dir"] : "ASC";

        if (isset($aPost["activ"]) && ($aPost["activ"] == 1)) {
            $this->db->where("tq_active", $aPost["activ"]);
        }

        if (isset($aPost["query"]) && ($aPost["query"] != "")) {
            $this->db->where("client.name", $aPost["query"]);
        }

        if (isset($aPost["client_id"]) && ($aPost["client_id"] != "")) {
            $this->db->where("tele_question.client_id", $aPost["client_id"]);
        }

        $this->db->join("client", "client.client_id=tele_question.client_id", "left");
        $this->db->order_by($sSort, $sDir);
        $this->db->limit($iLimit, $iStart);
        $query = $this->db->get("tele_question");

        $totalCount = $this->db->count_all_results("tele_question");
        $aData = $query->result_array();
        $this->db->flush_cache();
        $this->db->stop_cache();

        $data = array('totalCount' => $totalCount, 'data' => $aData);
        return $data;
    }


     /**
     * Functie care aduce datele in grid
     */
    function getQuestionAnswer($aPost) {

        $this->db->start_cache();
        $iStart = (isset($aPost["start"]) && ($aPost["start"] != "")) ? $aPost["start"] : 0;
        $iLimit = (isset($aPost["limit"]) && ($aPost["limit"] != "")) ? $aPost["limit"] : 100;
        $sSort = (isset($aPost["sort"]) && ($aPost["sort"] != "")) ? $aPost["sort"] : "tqa_name";
        $sDir = (isset($aPost["dir"]) && ($aPost["dir"] != "")) ? $aPost["dir"] : "ASC";

        if (isset($aPost["activ"]) && ($aPost["activ"] == 1)) {
            $this->db->where("tq_active", $aPost["activ"]);
        }

        if (isset($aPost["tq_id"]) && ($aPost["tq_id"] != "")) {
            $this->db->where("tq_id", $aPost["tq_id"]);
        }

        $this->db->order_by($sSort, $sDir);
        $this->db->limit($iLimit, $iStart);
        $query = $this->db->get("tele_question_answer");

        $totalCount = $this->db->count_all_results("tele_question_answer");
        $this->db->flush_cache();
        $this->db->stop_cache();
        $aData = $query->result_array();

        $data = array('totalCount' => $totalCount, 'data' => $aData);
        return $data;
    }


    /**
     * Adaugare / editare
     * @param  $aData
     * @return bool|string
     */
    function operation($aData) {
        $aTemp = array(
            "tq_name" => $aData["tq_name"],
            "client_id" => $aData["client_id"],
            "tq_active" => $aData["tq_active"]
        );
        //verifica duplicatele ( vezi dbase in dir library)
        $bDuplicate = $this->_verifyDuplicat($aData["tq_id"], $aTemp);
        if ($bDuplicate === TRUE) {
            return "Duplicat";
        }
        //insert
        if ($aData["tq_id"] == 0) {
            $this->db->insert("tele_question", $aTemp);
        }
        else {
            $this->db->where("tq_id", $aData["tq_id"]);
            $this->db->update("tele_question", $aTemp);
        }
        return TRUE;
    }

    /**
     * Adaugare / editare
     * @param  $aData
     * @return bool|string
     */
    function operationAnswer($aData) {
        $aTemp = array(
            "tqa_name" => $aData["tqa_name"],
            "tq_id" => $aData["tq_id"],
            "tqa_active" => $aData["tqa_active"]
        );
        //verifica duplicatele ( vezi dbase in dir library)
        $bDuplicate = $this->_verifyDuplicatAnswer($aData["tqa_id"], $aTemp);
        if ($bDuplicate === TRUE) {
            return "Duplicat";
        }
        //insert
        if ($aData["tqa_id"] == 0) {
            $this->db->insert("tele_question_answer", $aTemp);
        }
        else {
            $this->db->where("tqa_id", $aData["tqa_id"]);
            $this->db->update("tele_question_answer", $aTemp);
        }
        return TRUE;
    }

    function _verifyDuplicat($iRecord, $aData) {
        $this->db->where("tq_name",$aData["tq_name"]);
        $this->db->where("client_id",$aData["client_id"]);
        $this->db->where("tq_id !=", $iRecord);
        $iNumRows = $this->db->get("tele_question")->num_rows();
        if($iNumRows > 0) {
            return TRUE;
        }
        return FALSE;
    }

    function _verifyDuplicatAnswer($iRecord, $aData) {
        $this->db->where("tqa_name",$aData["tqa_name"]);
        $this->db->where("tq_id",$aData["tq_id"]);
        $this->db->where("tqa_id !=", $iRecord);
        $iNumRows = $this->db->get("tele_question_answer")->num_rows();
        if($iNumRows > 0) {
            return TRUE;
        }
        return FALSE;
    }


    /**
     * Functie care aduce datele despre o inregistrare
     */
    function getRecord($aData) {
        $this->db->select("tele_question.*, name as client_id_val");
        $this->db->join("client","client.client_id=tele_question.client_id","left");
        $this->db->where("tq_id", $aData["iRec"]);
        return $this->db->get("tele_question")->row_array();
    }

     /**
     * Functie care aduce datele despre o inregistrare
     */
    function getAnswer($aData) {
        $this->db->where("tqa_id", $aData["iRec"]);
        return $this->db->get("tele_question_answer")->row_array();
    }



}